package com.maxlen.camunda.controller;

import com.maxlen.camunda.constant.CamundaConstant;
import com.maxlen.camunda.entity.AttributeEntity;
import com.maxlen.camunda.entity.FormEntity;
import com.maxlen.camunda.entity.dto.SaveFormDTO;
import com.maxlen.camunda.service.AttributeService;
import com.maxlen.camunda.service.FormService;
import com.tcloudit.common.core.web.domain.AjaxResult;
import com.tcloudit.common.core.web.domain.PageEntity;
import com.tcloudit.common.core.web.page.TableDataInfo;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/camunda/form")
public class FormController {

    @Autowired
    private FormService formService;

    /**
     * 获取表单模型列表
     * @param page 分页搜索参数
     * @return 结果
     */
    @GetMapping("/list")
    public AjaxResult list(PageEntity<String> page) {
        TableDataInfo list = formService.getList(page);
        return AjaxResult.success(list);
    }

    /**
     * 获取状态为开启的表单模型列表
     * @param page 分页搜索参数
     * @return 结果
     */
    @GetMapping("/getStartFormList")
    public AjaxResult getStartFormList(PageEntity<String> page) {
        TableDataInfo list = formService.getStartFormList(page);
        return AjaxResult.success(list);
    }

    /**
     * 获取表单模型详情
     * @param id id
     * @return 结果
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable String id) {
        FormEntity formEntity = formService.info(id);
        return AjaxResult.success(formEntity);
    }

    /**
     * 新增表单模型
     * @param formEntity 实体类
     * @return 结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody FormEntity formEntity) {
        if(formEntity.getFormId().contains(CamundaConstant.FORM_SPLIT)){
            return AjaxResult.error("标识不允许存在.号");
        }
        int rows = formService.add(formEntity);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改表单模型
     * @param formEntity 实体类
     * @return 结果
     */
    @PutMapping
    public AjaxResult edit(@RequestBody FormEntity formEntity) {
        int rows = formService.edit(formEntity);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除表单模型
     * @param ids 部署id集合
     * @return 结果
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        int rows = formService.remove(ids);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 根据表单标识获取记录数
     * @param formEntity 表单标识和ID
     * @return 结果
     */
    @PostMapping("/getCountByFormId")
    public AjaxResult getCountByFormId(@RequestBody FormEntity formEntity){
        int count = formService.getCountByFormId(formEntity);
        return AjaxResult.success(count);
    }

    /**
     * 根据部署ID获取表单数据
     * @param deploymentId 部署ID
     * @return 结果
     */
    @GetMapping("/getFormByte/{deploymentId}")
    public AjaxResult getFormByte(@PathVariable String deploymentId) {
        String form = formService.getFormByte(deploymentId);
        return AjaxResult.success("", form);
    }

    /**
     * 保存表单信息
     * @param saveFormDTO 表单json和表单id
     * @return 结果
     */
    @PostMapping("/saveForm")
    public AjaxResult saveForm(@RequestBody SaveFormDTO saveFormDTO) {
        try {
            int row = formService.saveForm(saveFormDTO);
            return row > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e){
            return AjaxResult.error();
        }
    }

}
